$(document).ready(function(){

    //注销
        $(".menulist .list_out").click(function(){
    
            localStorage.setItem("sta",1);
            location.href="./Login.html";
        });
    


        $.ajax({
            type:"POST",
            url:"./GetCityServlet",
            data:"",
            success:function(json){
              var msg=JSON.parse(json);
              $(".searchlist .fromcity").empty();
              $(".searchlist .tocity").empty();
               if(msg!=null&&msg.infoList!=null)
               {
                   var html;
                   for(var i=0;i<msg.infoList.length;i++)
                   {
                       html+="<option value='"+msg.infoList[i].cityName+"'>"+msg.infoList[i].cityName+"</option>";
                   }
                   $(".searchlist .fromcity").append(html);
                   $(".searchlist .tocity").append(html);
               }
            }
        });

        $(".change .changeicon").click(function(){
            var dd=$(".searchlist .fromcity").val();
            $(".searchlist .fromcity").val($(".searchlist .tocity").val());
            $(".searchlist .tocity").val(dd);
        });
 
        $(".searchlist .searchbutton").click(function(){
            var fromcity=$(".searchlist .fromcity").val();
            var tocity=$(".searchlist .tocity").val();
            var fromdate=$(".searchlist .fromdate").val();
            var todate=$(".searchlist .todate").val();
         
            searchlist(fromcity,tocity,fromdate,todate);
           
        });
       
    
        $(".pagemsg .step-forward").click(function(){
            if(pageobj.page<pageobj.pages-1)
            {
                pageobj.page=pageobj.pages-1;
                searchlist(pageobj.name,pageobj.roleid,pageobj.page);
            }
        });
    
        $(".editbutton").click(function(){
          
            if($(".formclass table tbody tr.tdclass").length>0)
            {
                var scheduleid=$(".formclass table tbody tr.tdclass:eq(0)").attr("scheduleid");
                var sta=$(".formclass table tbody tr.tdclass td:eq(8)").text();
               if(sta=="Confirmed")
               {
                   sta="Cancel";
               }
               else{
                sta="Confirmed";
               }

               $.ajax({
                type:"POST",
                url:"./UpdateServlet",
                data:"scheduleid="+scheduleid+"&sta="+sta,
                success:function(json){
                    var msg=JSON.parse(json);
                    if(msg.flag=="success")
                    {
                        var fromcity=$(".searchlist .fromcity").val();
                        var tocity=$(".searchlist .tocity").val();
                        var fromdate=$(".searchlist .fromdate").val();
                        var todate=$(".searchlist .todate").val();
         
                         searchlist(fromcity,tocity,fromdate,todate);
                    }
    
                   
                }
            });
            }
    
          
        });
     
    });
    
    

    function  searchlist(fromcity,tocity,fromdate,todate){
    
        $.ajax({
            type:"POST",
            url:"./getScheservlet",
            data:"fromcity="+fromcity+"&tocity="+tocity+"&fromdate="+fromdate+"&todate="+todate,
            success:function(json){
                var msg=JSON.parse(json);
                $(".formclass table tbody tr").remove();
                if(msg!=null&&msg.infoList!=null)
                {
                    for(var i=0;i<msg.infoList.length;i++)
                    {
                       
    
                        var html="<tr class='tdcolor' onclick='trclick(this)' scheduleid='"+msg.infoList[i].scheduleid+"'><td>"+
                        msg.infoList[i].date+"</td><td>"+
                        msg.infoList[i].time+"</td><td>"+
                        msg.infoList[i].depart+"</td><td>"+
                        msg.infoList[i].arrive+"</td><td>"+
                        msg.infoList[i].name+"</td><td>"+
                        msg.infoList[i].price+"</td><td>"+
                        msg.infoList[i].flight+"</td><td>"+
                        msg.infoList[i].gate+"</td><td>"+
                        msg.infoList[i].status+"</td><td onclick='tdclick(this)'>detail</td></tr>";
                        $(".formclass table tbody").append(html);
                        $(".formclass table tbody tr:contains(Cancel)").css("background-color","red");
                    }
    
                  
    
                
                }
            }
        });
    }
    
    function trclick(obj){
        $(".formclass table tbody tr").removeClass("tdclass");
        $(obj).addClass("tdclass");
    }

    function tdclick(obj){
        var scheduleid=$(obj).parent().attr("scheduleid");
        localStorage.setItem("scheduleid",scheduleid);
        location.href="./TicketSalesDetail.html";
    }